import sys
from PyQt5.QtWidgets import (
    QVBoxLayout, QToolTip, QMessageBox,
    QPushButton, QLabel, QWidget, QDialog,
    QApplication, QLineEdit, QFormLayout
)

from PyQt5.QtGui import QPalette, QColor, QPixmap, QIntValidator, QDoubleValidator
from PyQt5.QtCore import Qt

class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.initUI()

    def initUI(self):
        
        self.setGeometry(300, 300, 600, 600)#设置窗口位置和大小
        self.setWindowTitle('QWidget 实例')#设置窗口标题
        
        formLayout = QFormLayout()#创建一个form layout
        
        pIntLineEdit = QLineEdit(self)
        pDoubleLineEdit = QLineEdit(self)
        pValidatorLineEdit = QLineEdit(self)
        
        formLayout.addRow("整数类型", pIntLineEdit)
        formLayout.addRow("浮点类型", pDoubleLineEdit)
        formLayout.addRow("验证器类型", pValidatorLineEdit)
        
        pIntLineEdit.setPlaceholderText("请输入整数")
        pDoubleLineEdit.setPlaceholderText("请输入浮点数")
        pValidatorLineEdit.setPlaceholderText("请输入验证器")
        
        pIntVAlidator = QIntValidator(self)#创建一个整数验证器
        pIntVAlidator.setRange(0, 100)#设置范围0-100
        pDoubleValidator = QDoubleValidator(self)#创建一个浮点数验证器
        pDoubleValidator.setRange(0.0, 100.0)#设置范围0.0-100.0
        
        pDoubleValidator.setNotation(QDoubleValidator.StandardNotation)#设置标准格式
        pDoubleValidator.setDecimals(2)#设置小数点后2位
        
        pIntLineEdit.setValidator(pIntVAlidator)#设置验证器
        pDoubleLineEdit.setValidator(pDoubleValidator)#设置验证器
        pValidatorLineEdit.setValidator(pIntVAlidator)#设置验证器

        self.setLayout(formLayout)#设置窗口布局


def main():
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
    
    
if __name__ == "__main__":
    main()
